<template>
  <div>
    <h2>登录</h2>
    <table class="table table-bordered">
      <tr>
        <td>用户名：</td>
        <td><input type="text" v-model="state.L_Name" class="form-control" ></td>
      </tr>
      <tr>
        <td>密码：</td>
        <td><input type="password"  v-model="state.L_Pwd" class="form-control">
        </td>
      </tr>
      <tr>
        <td colspan="2"><button @click="login" class="btn btn-primary">登录</button></td>
      </tr>

    </table>
  </div>
</template>
<script setup lang="ts">
import {reactive,toRefs} from 'vue'
import axios from 'axios'
import {useRouter} from 'vue-router'
const router=useRouter()
import {useStore} from '../../store/user'
const store=useStore()

const data=reactive({
  state:{
    L_id:0,
    L_Name:'',
    L_Pwd:''
  }
})

const {state}=toRefs(data)

const login=()=>{

  axios({
    url:'http://localhost:59121/api/Finance/GetFinanceLogin',
    method:'get',
    params:state.value
  })
  .then((res)=>{
    console.log(res.data)
    store.UserInfo=res.data
    if(res.data==1){
      alert('账号不存在')
      return
    }
    if(res.data==2){
      alert('密码错误')
      return
    }
    if(res.data!=null){
      axios({
        url:'http://localhost:59121/api/Finance/GetFinanceUserType',
        method:'get',
        params:{
          L_id:res.data.L_id
        }
      })
       .then((ult)=>{
        console.log(ult.data)
        if(ult.data.T_Name=="管理员"){
          alert(ult.data.L_Name+'管理员登陆成功')
          router.push('/AdminFinance')
        }
        if(ult.data.T_Name=="普通用户"){
          alert(ult.data.L_Name+'普通用户登陆成功')
          router.push('/UserFinance')
        }
       })
       .catch((err)=>{
        console.log(err)
       })
    }
  })
  .catch((err)=>{
    console.log(err)
  })
}

</script>